//
//  KCPatrolRepTipCell.swift
//  MyKC
//
//  Created by yy on 2017/3/30.
//  Copyright © 2017年 com.yasuo. All rights reserved.
//
import UIKit

@objc protocol KCPatrolRepTipCellDelegate: NSObjectProtocol {
    @objc optional func repTipCellDidSelect(indexPath: IndexPath)
}

class KCPatrolRepTipCell: UITableViewCell {
    weak var delegate: KCPatrolRepTipCellDelegate?
    @IBOutlet weak var lbe_questionTip: UILabel!
    @IBOutlet weak var btn_selectQuestion: UIButton!
    lazy var indexPath:IndexPath = IndexPath()
    lazy var answerArray = [String]()
    var questionModel: KCQuestionModel? {
        //外界已经完成了的赋值
        didSet {
            answerArray = questionModel?.Array ?? [String]()
            if indexPath.row == 1 {
                lbe_questionTip.text = questionModel?.A
            }
            else if indexPath.row == 2 {
                lbe_questionTip.text = questionModel?.B
            }
            else if indexPath.row == 3 {
                lbe_questionTip.text = questionModel?.C
            }
            else if indexPath.row == 4 {
                lbe_questionTip.text = questionModel?.D
            }
            else if indexPath.row == 5 {
                lbe_questionTip.text = questionModel?.E
            }
            else if indexPath.row == 6 {
                lbe_questionTip.text = questionModel?.F
            }
            for answer in answerArray {
                if answer.hasPrefix("A:") && indexPath.row == 1 || answer.hasPrefix("B:") && indexPath.row == 2 || answer.hasPrefix("C:") && indexPath.row == 3 || answer.hasPrefix("D:") && indexPath.row == 4 || answer.hasPrefix("E:") && indexPath.row == 5 || answer.hasPrefix("F:") && indexPath.row == 6{
                    btn_selectQuestion.isSelected = true
                }else {
                    btn_selectQuestion.isSelected = false
                }
            }
            //因为不进入数组所以判断出错
            if answerArray.count == 0 {
                btn_selectQuestion.isSelected = false
            }
        }
    }
    
    @IBAction func btnSelectClick(_ sender: UIButton) {
        //这个可以用  但是要设置其他的不可用
        let questionDesc = getQuestionDesc(indexPath: indexPath)
        if questionModel?.QuestionType == "800001"{
            if sender.isSelected == true {
                //根据当前的indexPath 来判断数组中的数据-------
                answerArray.removeAll()
                //刷新数据
            }
            else {
                answerArray.removeAll()
                answerArray.append(questionDesc)
            }
        }
        else if questionModel?.QuestionType == "800002" {
            if sender.isSelected == true {
                //刷新数据
                for (index,answer) in answerArray.enumerated() {
                    if answer == questionDesc {
                        answerArray.remove(at: index)
                    }
                }
            }
            else {
                answerArray.append(questionDesc)
            }
        }
        questionModel?.Array = answerArray
        
//        for answer in answerArray {
//            if answer.hasPrefix("A:") && indexPath.row == 1 || answer.hasPrefix("B:") && indexPath.row == 2 || answer.hasPrefix("C:") && indexPath.row == 3 || answer.hasPrefix("D:") && indexPath.row == 4 || answer.hasPrefix("E:") && indexPath.row == 5 || answer.hasPrefix("F:") && indexPath.row == 6{
//                btn_selectQuestion.isSelected = true
//            }else {
//                btn_selectQuestion.isSelected = false
//            }
//        }
        
        delegate?.repTipCellDidSelect?(indexPath: indexPath)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        btn_selectQuestion.setImage(UIImage(named: "选中"), for: .selected)
    }

    private func getQuestionDesc(indexPath: IndexPath) -> String {
        if indexPath.row == 1 {
            return "A:" + (questionModel?.A)!
        }
        else if indexPath.row == 2 {
            return "B:" + (questionModel?.B)!
        }
        else if indexPath.row == 3 {
            return "C:" + (questionModel?.C)!
        }
        else if indexPath.row == 4 {
            return "D:" + (questionModel?.D)!
        }
        else if indexPath.row == 5 {
            return "E:" + (questionModel?.E)!
        }
        else if indexPath.row == 6 {
            return "F:" + (questionModel?.F)!
        }
        return ""
    }
}
